package aishua.com.mytestapplist.bean;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import aishua.com.mytestapplist.coredata.ApduInfo;


public class ResAppletApduChatBean extends BaseBeanRes {
	private String version;
	private String traceNo;
	private String operationResult;
	private String resultDes;
	private String finishFlag;
	
	public String getTraceNo() {
		return traceNo;
	}

	public void setTraceNo(String traceNo) {
		this.traceNo = traceNo;
	}

	public String getOperationResult() {
		return operationResult;
	}

	public void setOperationResult(String operationResult) {
		this.operationResult = operationResult;
	}

	public String getResultDes() {
		return resultDes;
	}

	public void setResultDes(String resultDes) {
		this.resultDes = resultDes;
	}

	public String getFinishFlag() {
		return finishFlag;
	}

	public void setFinishFlag(String finishFlag) {
		this.finishFlag = finishFlag;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	/** ���������ؿͻ�������ƬApduָ� */
	private List<ApduInfo> resApduInfoList;
	
	private String taskIndex;
	
	

	public List<ApduInfo> getResApduInfoList() {
		return resApduInfoList;
	}

	public void setResApduInfoList(List<ApduInfo> resApduInfoList) {
		this.resApduInfoList = resApduInfoList;
	}

	public String getTaskIndex() {
		return taskIndex;
	}

	public void setTaskIndex(String taskIndex) {
		this.taskIndex = taskIndex;
	}

	public void parserXmlPackage(String xmlPackage) {

		resApduInfoList = new ArrayList<ApduInfo>();
		ByteArrayInputStream is = null;
		try {
			is = new ByteArrayInputStream(xmlPackage.getBytes("UTF-8"));
			XmlPullParser xParser = XmlPullParserFactory.newInstance()
					.newPullParser();
			xParser.setInput(is, "UTF-8");
			
			ApduInfo apduInfo = null;
			
			int eventType = xParser.getEventType();
			while (eventType != XmlPullParser.END_DOCUMENT) {

				String tagName = xParser.getName();
				if (tagName != null && !tagName.equals("")) {
					
					if ("tsm".equals(tagName)
							&& eventType == XmlPullParser.START_TAG) {
						setVersion(xParser.getAttributeValue("", "version"));
					} else if ("keyVersion".equals(tagName)) {
						setKeyVersion(xParser.nextText());
					} else if ("keyIndex".equals(tagName)) {
						setKeyIndex(xParser.nextText());
					} else if ("clientType".equals(tagName)) {
						setClientType(xParser.nextText());
					} else if ("business".equals(tagName)
							&& eventType == XmlPullParser.START_TAG) {
						
						setBusinessType(xParser.getAttributeValue("", "type"));
						eventType = xParser.next();
						while (eventType != XmlPullParser.END_TAG) {
							tagName = xParser.getName();
							if ("traceNo".equals(tagName)) {
								setTraceNo(xParser.nextText());
							} else if ("operationResult".equals(tagName)) {
								setOperationResult(xParser.nextText());
							} else if ("resultDes".equals(tagName)) {
								setResultDes(xParser.nextText());
							} else if ("finishFlag".equals(tagName)) {
								setFinishFlag(xParser.nextText());
							} else if ("CAPDUList".equals(tagName) 
									&& eventType == XmlPullParser.START_TAG) {
								eventType = xParser.next();
								while (eventType != XmlPullParser.END_TAG) {
									tagName = xParser.getName();
									if ("CAPDUInfomation".equals(tagName) 
										&& eventType == XmlPullParser.START_TAG) {
										apduInfo = new ApduInfo();
										List<String> swList = new ArrayList<String>();
										eventType = xParser.next();
										while (eventType != XmlPullParser.END_TAG) {
											tagName = xParser.getName();
											if ("index".equals(tagName)) {
												apduInfo.setIndex(Integer.valueOf(xParser.nextText()));
											} else if("APDU".equals(tagName)) {
												apduInfo.setApdu(xParser.nextText());
											} else if("SW".equals(tagName)) {
												swList.add(xParser.nextText());
											} else {
												xParser.nextText();
											}
											eventType = xParser.next();
										} 
										apduInfo.setSwList(swList);
										resApduInfoList.add(apduInfo);
									} 
									eventType = xParser.next();
								}
							} else if ("TaskIndex".equals(tagName)) {
								setTaskIndex(xParser.nextText());
							} else {
								xParser.nextText();
							}
							eventType = xParser.next();
						}
					} else if ("mac".equals(tagName)) {
						setMac(xParser.nextText());
					} else if ("tsm".equals(tagName)
							&& eventType == XmlPullParser.END_TAG) {

					} else {
						xParser.nextText();
					}

				}
				eventType = xParser.next();
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (is != null) {
					is.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
	}



	
}
